package com.yixia.system.service.impl.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixia.common.constant.UserConstants;
import com.yixia.common.core.domain.AjaxResult;
import com.yixia.common.exception.ServiceException;
import com.yixia.common.utils.StringUtils;
import com.yixia.common.utils.page.PageBean;
import com.yixia.system.domain.system.SysPost;
import com.yixia.system.domain.system.SysUserPost;
import com.yixia.system.mapper.system.SysPostMapper;
import com.yixia.system.mapper.system.SysUserPostMapper;
import com.yixia.system.service.system.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 35038
* @description 针对表【sys_post(岗位信息表)】的数据库操作Service实现
* @createDate 2023-08-05 23:09:35
*/
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost>
    implements SysPostService {

    @Autowired
    private SysUserPostMapper userPostMapper;
    @Autowired
    private SysPostMapper postMapper;

    @Override
    public List<SysPost> selectPostAll() {
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPost::getDelFlag, '0');
        return this.list(queryWrapper);
    }

    /**
     * 根据userId获取对应岗位Ids
     */
    @Override
    public List<Long> selectPostIdsByUserId(Long userId) {
        /*1. 先查询角色岗位表，获取岗位ID集合*/
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysUserPost::getPostId).eq(SysUserPost::getUserId, userId);
        List<SysUserPost> sysUserPosts = userPostMapper.selectList(queryWrapper);
        List<Long> collect = sysUserPosts.stream().map(SysUserPost::getPostId).collect(Collectors.toList());
        return collect;
    }

    /**
     * 进行分页查询
     * @param pageBean 分页
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public AjaxResult selectPostPageList(PageBean pageBean, SysPost post) {

        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(post.getPostId()), SysPost::getPostId, post.getPostId())
                .eq(StringUtils.isNotEmpty(post.getStatus()), SysPost::getStatus, post.getStatus())
                .like(StringUtils.isNotEmpty(post.getPostName()), SysPost::getPostName, post.getPostName())
                .like(StringUtils.isNotEmpty(post.getPostCode()), SysPost::getPostCode, post.getPostCode())
                .eq(SysPost::getDelFlag, '0');
        /*排序*/
        queryWrapper.orderByAsc(SysPost::getPostSort);
        /*构建分页器*/
        Page<SysPost> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        /*进行分页查询*/
        Page<SysPost> pageInfo = postMapper.selectPage(page, queryWrapper);
        /*封装并进行返回*/
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", pageInfo.getRecords());
        ajax.put("total", pageInfo.getTotal());
        return ajax;
    }

    /**
     * 检验岗位名称是否唯一
     * @param post 岗位
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 校验岗位编码是否唯一
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostCodeUnique(post.getPostCode());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增岗位
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost post) {
        return postMapper.insert(post);
    }

    /**
     * 修改岗位
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost post) {
        return postMapper.updateById(post);
    }

    /**
     * 删除岗位
     * @param postIds 岗位IDS
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds) {

        /*1. 需要判断该岗位是否已经被使用了*/
        for (Long postId : postIds) {
            SysPost post = postMapper.selectById(postId);
            if(countUserPostByPostId(postId) > 0){
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        /*2. 没有绑定，则进行删除*/
        return postMapper.deletePostByPostIds(postIds);
    }

    /**
     * 根据postId来看有多少用户使用该岗位
     * @param postId 岗位ID
     * @return 结果
     */
    public int countUserPostByPostId(Long postId) {
        return userPostMapper.countUserPostByPostId(postId);
    }


}




